package com.example.mvvm

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.asLiveData
import com.atom.module.mvvm.base.AbstractViewModelFactory
import com.atom.module.mvvm.base.AbstractBaseActivity
import com.atom.module.mvvm.extren.clicks
import com.atom.module.utils.Logs
import com.example.mvvm.databinding.ActivityMainBinding
import kotlinx.coroutines.flow.*
import timber.log.Timber
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : AbstractBaseActivity<ActivityMainBinding, MainViewModel>() {

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.activity_main

    override fun initViewModel(): MainViewModel {
        val factory = object : AbstractViewModelFactory() {
            override fun <T : ViewModel?> create(modelClass: Class<T>): T {
                if (modelClass.isAssignableFrom(MainViewModel::class.java)) {
                    return MainViewModel() as T
                }
                return modelClass.newInstance()
            }
        }
        return ViewModelProvider(this, factory).get(MainViewModel::class.java)
    }

    override fun initVariableId(): Int {
        return BR.mainBinding
    }

    @SuppressLint("SimpleDateFormat")
    override fun initViewBinding(binding: ActivityMainBinding, viewModel: MainViewModel) {
        super.initViewBinding(binding, viewModel)
        binding.mainBindingTest = ViewBindingTest()
        binding.also {
//            it.testFlowCallback.clicks().onEach {
//                Thread {
//                    it.post {
//                        Toast.makeText(it.context, "haha", Toast.LENGTH_SHORT).show()
//                    }
//                }.start()
//            }.launchIn(this.lifecycleScope)

            val asLiveData = it.testFlowCallback.clicks().onEach {
                Thread {
                    it.post {
                        Toast.makeText(it.context, "haha1", Toast.LENGTH_SHORT).show()
                    }
                }.start()
            }.asLiveData()

            asLiveData.observe(this, {
                //Logs.e("testFlowCallback")
                Timber.e(
                    "%s ,%s ,%s ",
                    "testFlowCallback 1",
                    "testFlowCallback 2",
                    "testFlowCallback 3"
                )
            })
        }

        binding.testFlowCallback1.setOnClickListener {
            val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            var value = "2021-12-31 00:00:00"
            var parse = formatter.parse(value) ?: return@setOnClickListener
            var cal = Calendar.getInstance()
            cal.time = parse
            Logs.e("%s", getDayByWeekend(cal[Calendar.DAY_OF_WEEK]-1))
            value = "2022-01-01 00:00:00"
            parse = formatter.parse(value) ?: return@setOnClickListener
            cal = Calendar.getInstance()
            cal.time = parse
            Logs.e("%s", getDayByWeekend(cal[Calendar.DAY_OF_WEEK]-1))
        }
    }

    fun getDayByWeekend(day: Int): String {
        return when (day) {
            1 -> "周一"
            2 -> "周二"
            3 -> "周三"
            4 -> "周四"
            5 -> "周五"
            6 -> "周六"
            else -> "周日"
        }
    }

}